package shangke;
import java.io.*;
import java.util.Arrays;
public class day1 {
    public static void main(String[] args) throws IOException {
        //写入流
        Writer writer=null;
        //字符写入流
        BufferedWriter bufferedWriter=null;
        String str="中国共产党万岁，人民万岁";
        //新建文件
        File file=null;
        try{
            //新建文件
             file=new File("D:\\WUDownloadCache\\54.txt");
             //append:为true时，程序执行一次，每次添加;为flase时：程序执行一次，每次重新添加
            //往文件写
            //字符流
            writer=new FileWriter(file,false);
             bufferedWriter=new BufferedWriter(writer);
             bufferedWriter.write(str);
            bufferedWriter.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
           if(writer!=null){
               writer.close();
           }
           if(bufferedWriter!=null){
               bufferedWriter.close();
           }
        }
        //方法一
        System.out.println(str);
        //方法二:通过读来输出
        Reader reader=new FileReader(file);
        BufferedReader bufferedReader=new BufferedReader(reader);
        String str1=bufferedReader.readLine();
        bufferedReader.close();
        System.out.println(str1);
        //新建字符输出流
        /*Writer writer=null;
        try{
            //新建文件
            File file =new File("D:\\WUDownloadCache\\53.txt");
            //往哪个文件写
            writer=new FileWriter(file);
            //准备写入的内容
            String str="Java语言开发很难";
            //写进去
            writer.write(str);
            //刷新缓存
            writer.flush();
}catch (Exception e){
            e.printStackTrace();
        }finally {
            //关闭流
            writer.close();
        }*/
        //创建文件
        /*File file = new File("D:\\WUDownloadCache\\52.txt");
        //读
        Reader reader=new FileReader(file);
        //增强读的速率
     BufferedReader bufferedReader=new BufferedReader(reader);
     //读取一行数据，返回字符串
     String str=bufferedReader.readLine();
        //关闭流
     bufferedReader.close();
     //输出
     System.out.println(str);*/
     /*char[] ch=new char[1024];
        //字符串的缓冲区
        StringBuffer stringBuffer=new StringBuffer();
        int t =-1;
        while ((t = reader.read())!=-1){
            //强转和追加字符串
            stringBuffer.append((char) t);
        }
        //将Stringbuffer类型的字符串1转换为String类型的对象并返回;toString:对数组的输出
        System.out.println(stringBuffer.toString());*/
    }
}

